package com.junko.api.controller;

import com.junko.model.JsonResult;
import org.springframework.http.HttpStatus;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletResponse;

@ControllerAdvice
@RestController
@RequestMapping( value = "/error")
public class ControllerException {
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    @ResponseBody
    public JsonResult httpRequestMethodNotSupportedException(HttpServletResponse response) {
        response.setStatus(200);
        return JsonResult.fail(405, "请求方法不支持");
    }

    @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
    @ResponseBody
    public JsonResult httpMediaTypeNotSupportedException(HttpServletResponse response) {
        response.setStatus(200);
        return JsonResult.fail(415, "不支持媒体类型");
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public JsonResult notFoundPage404(HttpServletResponse response) {
        response.setStatus(200);
        return JsonResult.fail(404,"找不到请求资源");
    }

    @RequestMapping(value = "/401" ,produces = {"application/json;charset=UTF-8"})
    public JsonResult forbidden401(HttpServletResponse response) {
        response.setStatus(200);
        System.out.println("401");
        return JsonResult.fail(401,"没有访问权限");
    }

    @RequestMapping(value = "/403" ,produces = {"application/json;charset=UTF-8"})
    public JsonResult forbidden403(HttpServletResponse response) {
        response.setStatus(200);
        System.out.println("403");
        return JsonResult.fail(403,"没有访问权限");
    }
//
//    @ExceptionHandler(Exception.class)
//    @ResponseBody
//    public JsonResult defaultException(HttpServletResponse response) {
//        response.setStatus(200);
//        return JsonResult.fail(500, "网络异常");
//    }
}